package com.distribute.application.admin.permissions;

import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.core.lang.*;
import com.distribute.framework.core.web.node.Export;
import com.distribute.framework.core.lang.Protocol.BeanFormat;
import com.distribute.framework.core.util.Excel;
import com.distribute.framework.core.util.Function;
import com.distribute.framework.core.web.util.*;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import com.distribute.framework.model.*;
import com.distribute.application.admin.*;
/************CTRL_CODE_INPORT************/
/************CTRL_CODE_INPORT************/

/**
 * 权限配置的Controller
 * @author 飞啊飘啊
 */
@SuppressWarnings({"unchecked","rawtypes"})
@flag("SrvLimited")
@RestController
@Component(value="permissions.SrvLimitedController")
public class LimitController{
	
	@limitVal(1)//查看权限
	@flag("stuct")
	@RequestMapping("/admin/permissions/limit.list.do")
  	public Protocol list() {
  		try{
  			Protocol protocol = Protocol.create(true,null);
  			setFormDataInfo(protocol,null,null);
  			//处理页面的视图数据，比如显示哪些列，自定义显示列等
			ActionHelper.doPageViewFields(SrvLimited.class);
  			Map<String,Object> params = setDefaultSort(ActionHelper.getQueryFormParams(SrvLimited.class));
			protocol.set("PARAMS",ActionHelper.getQueryFormParams(SrvLimited.class));
			protocol.set("fields", Utils.getRequest("fields"));
			Integer iPage = Utils.getInteger("page", 0);
			Integer iSize = Utils.getInteger("size",16);
			/************CTRL_BEFORE_LIST************/
			protocol.set("groups", SrvGroup.where.sgStatus(1).list(-1),new BeanFormat<SrvGroup>(){
				public Map format(SrvGroup t) {
					return t.toJSON();
				}
			});
			Long group = Utils.getLong("group");
			if(group!=null){
				protocol.set("group", group);
				protocol.set("srvGroup",SrvGroup.get(group));
				//获取该对象父级秘支持的权限			
				List<SrvLimited> _limits=new java.util.ArrayList();//权限设置者的权限
				List _projects=new java.util.ArrayList();//权限设置者可见的项目
				
				//获取该对象所支持的项目列表	
				if(LoginHelper.getUserId().longValue()!=0){
					_limits = SrvLimited.where.slGroup(LoginHelper.getUser().getSuGroup()).list(65535);//取管理组权限
					for(SrvLimited o:_limits){
						_projects.add(o.get$slProject());
					}
				}else{//如果父类为空，说明直属超级用户
					protocol.set("EXTEND_BY_SUPER", true);
					_projects=SrvProject.where.definex("1=1").list(65535);
				}
				java.util.Collections.sort(_projects, new java.util.Comparator<SrvProject>(){
					public int compare(SrvProject o1,SrvProject o2){
						if(o1.getSpFamily().split(",").length<o2.getSpFamily().split(",").length)
							return 0;
						if(o1.getSpFamily().split(",").length==o2.getSpFamily().split(",").length && Utils.or(o1.getSpLevel(),0)>Utils.or(o2.getSpLevel(),0))
							return 0;
						return 1;
					}
				});
				
				//当前用户可看到的项目
				protocol.set("projects", _projects,new BeanFormat<SrvProject>(){
					public Map format(SrvProject t) {
						return t.toJSON();
					}
				});
				
				//权限设置者的权限
				protocol.set("limits", _limits,new BeanFormat<SrvLimited>(){
					public Map format(SrvLimited t) {
						return t.toJSON();
					}
				});
				
				//已经设置的权限
				List<SrvLimited> _limiteds = SrvLimited.where.slGroup(group).list(65535);
				protocol.set("limiteds", _limiteds,new BeanFormat<SrvLimited>(){
					public Map format(SrvLimited t) {
						return t.toJSON();
					}
				});
			}
			if(true){
				return protocol;
			}
			/************CTRL_BEFORE_LIST************/
			Page page = SrvLimited.page(iPage,iSize,params);//调用获取分页
	    	protocol.setPage(page,new BeanFormat<SrvLimited>(){
				public Map format(SrvLimited t) {
					return t.toJSON();
				}
			});
			/************CTRL_AFTER_LIST************/
			/************CTRL_AFTER_LIST************/
			return protocol;
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
  	}
  	
  	
  	@limitVal(1)//查看权限
  	@flag("stuct")
  	@RequestMapping("/admin/permissions/limit.get.do")
  	public Protocol get() {
  		try{
  			Protocol protocol = Protocol.create(true,null).set("srvLimited",new HashMap());
  			SrvLimited srvLimited = Utils.getObject("", SrvLimited.class);
  			if(srvLimited!=null){
				protocol.set("srvLimited",srvLimited.toJSON());
			}
			Long id=Utils.or(Utils.getLong("id"),Utils.getLong("key"));
			String edittype = Utils.getString("edittype");
			if(id==null){
				setFormDataInfo(protocol,edittype,null);
				protocol.reset(false,null,"args-id-null");
			}else{
				srvLimited=SrvLimited.where.slId(id).get();
				if(srvLimited==null){
					return protocol.reset(false,"数据不存在","data-not-exist");
				}
				setFormDataInfo(protocol,edittype,srvLimited);
				protocol.set("srvLimited",srvLimited.toJSON());
			}			
			/************CTRL_CODE_GET************/
			/************CTRL_CODE_GET************/
			return protocol;
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
  	}
  	  	
  	@limitVal(32)//导入权限
  	@RequestMapping("/admin/permissions/limit.inport.do")
  	public Protocol inport(@RequestParam("file") final MultipartFile file) { 
  		try{
			final String requestToken = Utils.getString("srvLimitedToken");
			final Date lastToken = Utils.getSession(requestToken);
			if(lastToken==null || (new Date().getTime()-lastToken.getTime())>10000){
				if(requestToken!=null)Utils.setSession(requestToken,new Date());//放入Token时间戳	
				SrvLimited.execute(new Function(){
					public <T> T execute(Object... arg0){
						/************CTRL_BEFOR_INPORT************/
						/************CTRL_BEFOR_INPORT************/
						SrvLimited srvLimited = Utils.getObject("srvLimited", SrvLimited.class);
						Excel excel = null;
						try {
							excel = Excel.parse(file.getInputStream(),file.getOriginalFilename());
						} catch (Exception e1) {
							e1.printStackTrace();
						}
						List<String> header = excel.getHeader(0);
						//检查列头信息是否正确
						{
							String[] array = Utils.getString("inport$").split(",");
							for(int j=0;j<header.size();j++){
								if(!Utils.equals(Utils.trim(array[j]),Utils.trim(header.get(j)))){
									throw new DefineException("列("+j+")头信息不正确");
								}
							}
						}
						List<List<String>> array = excel.getArray(0);
				        for(int i=0;i<array.size();i++){ 
							String[] inports = Utils.getString("inports").split(",");
							String sAll="";
							List<String> item = array.get(i);
				        	for(int j=0;j<inports.length;j++){
				        		try {
				        			sAll+=Utils.trim(item.get(j));
				        			SrvLimited.class.getMethod("set"+Utils.ForMat(inports[j])+"$", String.class).invoke(srvLimited, item.get(j));
				        		} catch (Exception e) {
									e.printStackTrace();
								}
				        	}
				        	if(Utils.empty(sAll))continue;
				        	//设置默认值
							setDefaultValue(srvLimited,"insert");
								/************CTRL_BEFORE_INPORT************/
								/************CTRL_BEFORE_INPORT************/
							try{
								SrvLimited.saveSrvLimited(srvLimited);			
							}catch(Exception ex){
								String strMessage = DefineException.parseException(ex,SrvLimited.class);
								if(strMessage!=null){
									throw new DefineException(strMessage);
								}else{
									throw new DefineException(ex);
								}
							}
							/************CTRL_AFTER_INPORT************/
							/************CTRL_AFTER_INPORT************/
				        }
						//记录操作日志
						LoggerHelper.writeLog(SrvLimited.class,"add","导入了[权限配置]数据",LoginHelper.getUserId(), srvLimited,srvLimited.getSlId());
						return null;
					}
				});
				
			}else{
				throw new DefineException("repeat-submit-error","对不起，请勿重复提交"+(10-((new Date().getTime()-lastToken.getTime())/1000))+"秒后再试");
			}
			/************CTRL_AFTER_INPORT************/
			/************CTRL_AFTER_INPORT************/
			return Protocol.create(true,"添加[权限配置]数据成功");
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
  	}
  	
  	@limitVal(2)//查看权限
  	@RequestMapping("/admin/permissions/limit.insert.do")
  	public Protocol insert() {
  		try{
			final String requestToken = Utils.getString("srvLimitedToken");
			final Date lastToken = Utils.getSession(requestToken);
			if(lastToken==null || (new Date().getTime()-lastToken.getTime())>10000){
				if(requestToken!=null)Utils.setSession(requestToken,new Date());//放入Token时间戳	
				SrvLimited.execute(new Function(){
					public <T> T execute(Object... arg0){
						/************CTRL_BEFOR_INSERT************/
						/************CTRL_BEFOR_INSERT************/
						SrvLimited srvLimited = Utils.getObject("srvLimited", SrvLimited.class);
						//批量插入处理
						String vValue = ""+(srvLimited.getSlProject()==null?"":srvLimited.getSlProject());
						String sArray[]={""+vValue};
						if(vValue.startsWith("[*]")){
							vValue=vValue.replace("[*]","");
							sArray=vValue.split("[\\s|\u3000|\\u00a0|,]+");		
						}
						for(String sItem:sArray){
							if(!Utils.empty(sItem))
							srvLimited.setSlProject(Long.valueOf(sItem));
							//设置默认值
							setDefaultValue(srvLimited,"insert");
								/************CTRL_BEFORE_ADD************/
								/************CTRL_BEFORE_ADD************/
							SrvLimited.saveSrvLimited(srvLimited);			
								/************CTRL_AFTER_ADD************/
								/************CTRL_AFTER_ADD************/
						}
						//记录操作日志
						LoggerHelper.writeLog(SrvLimited.class,"add","添加了[权限配置]["+srvLimited.getSlProject()+"]",LoginHelper.getUserId(), srvLimited,srvLimited.getSlId());
						return null;
					}
				});
				
			}else{
				throw new DefineException("repeat-submit-error","对不起，请勿重复提交"+(10-((new Date().getTime()-lastToken.getTime())/1000))+"秒后再试");
			}
			/************CTRL_AFTER_INSERT************/
			/************CTRL_AFTER_INSERT************/
			return Protocol.create(true,"添加[权限配置]数据成功");
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
  	}
  	
  	@limitVal(4)//修改权限
  	@RequestMapping("/admin/permissions/limit.update.do")
  	public Protocol update() {
  		try{
			final String requestToken = Utils.getString("srvLimitedToken");
			final Date lastToken = Utils.getSession(requestToken);
			if(lastToken==null || (new Date().getTime()-lastToken.getTime())>10000){
				if(requestToken!=null)Utils.setSession(requestToken,new Date());//放入Token时间戳	
				SrvLimited.execute(new Function(){
					public <T> T execute(Object... arg0){
						/************CTRL_BEFOR_UPDATE************/
						/************CTRL_BEFOR_UPDATE************/
						SrvLimited srvLimited = Utils.getObject("srvLimited", SrvLimited.class);
						SrvLimited oldSrvLimited = null;
						String PARAMS = Utils.getString("PARAMS");
						String ids=Utils.getString("ids");
						if(!Utils.empty(PARAMS)){//根据查询条件批量更新
							Map params = Utils.eval(PARAMS);
							params.put("confirm", 1);
							params.put("definex","1=1");
							SrvLimited.updateSrvLimitedByConfirm(ActionHelper.getSetValuesFromModel(srvLimited), params);
							LoggerHelper.writeLog(SrvLimited.class, "update", 
										"批量修改了[权限配置]",
										LoginHelper.getUserId(),LoggerHelper.getUpdateDescription(null,srvLimited,false)
										,null);
							/************CTRL_UPDATE_BY_PARAMS************/
							/************CTRL_UPDATE_BY_PARAMS************/
						}else if(!Utils.empty(ids)){//根据ids批量更新
							Map params = new HashMap();
							params.put("slIdIn",ids);
							params.put("confirm", 1);
							params.put("definex","1=1");
							SrvLimited.updateSrvLimitedByConfirm(ActionHelper.getSetValuesFromModel(srvLimited), params);
							LoggerHelper.writeLog(SrvLimited.class, "update", 
										"批量修改了[权限配置]",
										LoginHelper.getUserId(),LoggerHelper.getUpdateDescription(null,srvLimited,false)
										,null);
							/************CTRL_UPDATE_BY_IDS************/
							/************CTRL_UPDATE_BY_IDS************/
						}else{
							if(Utils.empty(Utils.getString("all"))){//修改对象非空的字段
								oldSrvLimited=SrvLimited.where.slId(srvLimited.getSlId()).get();
								if(oldSrvLimited==null){
									throw new DefineException("对不起，你没有权限修改当前数据");
								}
								
								SrvLimited.updateSrvLimited$NotNull(srvLimited);
								LoggerHelper.writeLog(SrvLimited.class, "update", 
										"修改了[权限配置]["+oldSrvLimited.getSlProject()+"]",
										LoginHelper.getUserId(),LoggerHelper.getUpdateDescription(oldSrvLimited, srvLimited,false)
										,srvLimited.getSlId());
								/************CTRL_UPDATE_NOT_NULL************/
								/************CTRL_UPDATE_NOT_NULL************/
							}else{//修改对象全部字段
								oldSrvLimited=SrvLimited.where.slId(srvLimited.getSlId()).get();
								if(oldSrvLimited==null){
									throw new DefineException("对不起，你没有权限修改当前数据");
								}
								//设置默认值
								srvLimited.setSlId(oldSrvLimited.getSlId());	
								srvLimited.setSlProject(oldSrvLimited.getSlProject());	
								srvLimited.setSlLimit(oldSrvLimited.getSlLimit());	
								//设置默认值
								setDefaultValue(srvLimited,"update");
								
								//从oldSrvLimited同步srvLimited未set过的值
								srvLimited.mergeSet(oldSrvLimited);
										
								SrvLimited.updateSrvLimited(srvLimited);
								LoggerHelper.writeLog(SrvLimited.class, "update", 
										"修改了[权限配置]["+oldSrvLimited.getSlProject()+"]",
										LoginHelper.getUserId(),LoggerHelper.getUpdateDescription(oldSrvLimited, srvLimited,true)
										,srvLimited.getSlId());
								/************CTRL_UPDATE_ALL************/
								/************CTRL_UPDATE_ALL************/
							}
						}
						/************CTRL_CODE_UPDATE************/
						/************CTRL_CODE_UPDATE************/
						return null;
					}
				});
				
			}else{
				throw new DefineException("repeat-submit-error","对不起，请勿重复提交"+(10-((new Date().getTime()-lastToken.getTime())/1000))+"秒后再试");
			}
			/************CTRL_AFTER_UPDATE************/
			/************CTRL_AFTER_UPDATE************/
			return Protocol.create(true,"修改[权限配置]数据成功");
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
  	}
  	
  	@limitVal(8)//删除权限
  	@RequestMapping("/admin/permissions/limit.delete.do")
  	public Protocol delete() {
  		try{
			SrvLimited.execute(new Function(){
				public <T> T execute(Object... arg0){
					Long pid=Utils.getLong("id");
					Long[] ids=new Long[]{pid};
					String strIds=Utils.getString( "ids");
					if(!Utils.empty(strIds) && !strIds.trim().equals("")){//批量删除
						String[] array = strIds.split(",");
						ids=new Long[array.length];
						for(int i=0;i<array.length;i++)
							ids[i]=Long.valueOf(array[i]);
					}
					if(ids.length==0){
						throw new DefineException("args-id-null","参数错误");
					}
					for(Long id:ids){
						SrvLimited srvLimited = null;
						srvLimited=SrvLimited.where.slId(id).get();
						if(srvLimited==null){
							throw new DefineException("对不起，你没有权限修改当前数据");
						}
						SrvLimited.delete(id);						
						if(srvLimited!=null)//记录操作日志
							LoggerHelper.writeLog(SrvLimited.class,"del","删除了[权限配置]["+srvLimited.getSlProject()+"]",LoginHelper.getUserId(), srvLimited);
						/************CTRL_CODE_DEL************/
						/************CTRL_CODE_DEL************/
					}
					return null;
				}
			});
			return Protocol.create(true,"删除操作成功");
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
  	}

	@RequestMapping("/admin/permissions/limit.query.do")
	public Protocol query(){
		try{
			Map<String,Object> params = setDefaultSort(ActionHelper.getQueryFormParams(SrvLimited.class));
			
			String strTerm=Utils.getString("value");//智能搜索时的参数，一般都是主键后面那个字段
			if(!Utils.empty(strTerm)){
				if(strTerm.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
					params.put("slId", strTerm.toLowerCase().replaceFirst("id:", ""));					
				else//按标识查询，模糊查询请带%
				{
					String strDefinex = "";
					strDefinex+=" or sl_project like '%"+strTerm.replaceAll("'", "''")+"%'";
					strDefinex="(2=1 "+strDefinex+")";
					params.put("definex",strDefinex);
				}
			}
			
			Integer iSize = Utils.getInteger("size",10);
			/************CTRL_CODE_QUERY************/
			/************CTRL_CODE_QUERY************/
			
			List<SrvLimited> list = SrvLimited.page(0,iSize,params).getResult();
			
			Boolean bObject=Utils.getBoolean("object",false);//是否需要整个数据对象
			List<Map> result = new java.util.ArrayList();			
			for(SrvLimited srvLimited:list){
				Map map = new HashMap();
				map.put("key",srvLimited.getId().toString());
				map.put("value", Utils.js(srvLimited.getKeyValue()));
				if(bObject==true){
					map.put("object",srvLimited.toJSON());
				}
				result.add(map);
			}
			/************CTRL_AFTER_QUERY************/
			/************CTRL_AFTER_QUERY************/
			
			return Protocol.create().setList(result);
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
	}
	/************CTRL_DEFINE_CODE************/
	@RequestMapping("/admin/permissions/limit.submit.do")
	public Protocol submit()
	{			
		Protocol protocol = Protocol.create(true,null);		
		try{
			Long group = Utils.getLong("group");
			String strLimitList=Utils.getString("limit_list");
			if(strLimitList==null || strLimitList.equals("")){
				return Protocol.create(false,"修改权限失败:无项目可设置。");
			}
			String[] aLister=strLimitList.split(",");
			if(aLister==null || aLister.length==0){
				return Protocol.create(false,"修改权限失败:无项目可设置。");
			}
			List<SrvLimited> dbs = SrvLimited.where.slGroup(group).list(65535);
			Map<Long,SrvLimited> exists = new HashMap();
			for(SrvLimited srvLimited:dbs){
				exists.put(srvLimited.getId(), srvLimited);
			}
			List<SrvLimited> limits=new java.util.ArrayList();
			for(int i=0;i<aLister.length;i++){
				SrvLimited limit=new SrvLimited();
				limit.setSlGroup(group);
				String strItemer=aLister[i];
				String[] aItemer=strItemer.split("\\|");
				Long lLimitId=Long.parseLong(aItemer[0]);
				Long lProjectId=Long.parseLong(aItemer[1]);
				Integer lLimitValue=Integer.parseInt(aItemer[2]);
				if(lLimitId.longValue()!=0)
					limit.setSlId(lLimitId);
				else
					limit.setSlId(null);
				limit.setSlProject(lProjectId);
				limit.setSlLimit(lLimitValue);
				if(limit.getSlId()==null && limit.getSlLimit().intValue()!=0){//新增的权限
					limits.add(limit);
				}else if(exists.get(limit.getSlId())==null && limit.getSlLimit().intValue()==0){
					//nothing;
				}else if(exists.get(limit.getSlId())!=null && limit.getSlLimit().intValue()==exists.get(limit.getSlId()).getSlLimit().intValue()){
					//已存在，示变更
				}else{//修改的权限
					limits.add(limit);
				}
			}
			
			//更新子节点所有节点的权限不能超过当前权限
			for(SrvLimited limited:limits){
				if(limited.getSlId()==null){
					limited.save();	
				}else{
					limited.update();
				}
			}
			//删除权限值为0的权限
			SrvLimited.where.slLimit(0).delete();
			protocol.setMessage("修改权限成功");
		}catch(DefineException e){
			return Protocol.create(false, e.getMessage(), e.getCode());
		}catch(Exception e){		
			e.printStackTrace();
			LoggerHelper.writeError(e);
			return Protocol.create(false,Utils.or(Utils.config("exception.message"), "系统繁忙，请稍后再试"),"exception");
		}
		return protocol;
	}
	/************CTRL_DEFINE_CODE************/
  	
  	/**获取子项列表**/
	public static Map getItems(){
		Map map = new HashMap();
		/************CTRL_DEFINE_ITEMS************/
		/************CTRL_DEFINE_ITEMS************/
		return map;
	}
  	
  	/**
	* 设置排序
	**/
  	private Map<String,Object> setDefaultSort(Map<String, Object> params) {
		String strAsc=SrvLimited.C.get(Utils.forMat(Utils.getString("asc")));//升序字段
		params.put("asc", strAsc);//放入查询条件
		String strDesc=SrvLimited.C.get(Utils.forMat(Utils.getString("desc")));//降序字段
		params.put("desc",strDesc);//放入查询条件 
		if(Utils.empty(strAsc) && Utils.empty(strDesc))//如果未传入排序字段
			params.put("asc","sl_id");//那么，默认以主键降序，传给Dao层
		return params;
	}
  	/**
	* 设置默认值
	**/
	public void setDefaultValue(SrvLimited srvLimited,String type){
		if(type.equals("insert")){//添加时
		}else if(type.equals("update")){//编辑时
		}
	}
	/**
	* 设置表单信息
	**/
	public void setFormDataInfo(Protocol protocol,String edittype,SrvLimited srvLimited){
		Map model = new HashMap();
		model.put("name","srvLimited");
		model.put("caption","权限配置");
		model.put("type","table");
		model.put("icon","icon-yaochi1");
		model.put("PK","slId");
		model.put("add",true);
		model.put("update",true);
		model.put("details",true);
		List propertys = new ArrayList();
		List submits = new ArrayList();
		model.put("propertys", propertys);
		model.put("submits", submits);	
		protocol.set("model", model);
		if(Utils.empty(edittype)){
		}
		else{
			if(!(edittype.equals("details")?"readonly":(edittype.equals("insert")?"":"")).equals("none")){
				propertys.add(Utils.add("name","slProject").add("caption","所属项目").add("input","hidden").add("primary",false).add("length",8).add("notNull",true).add("unique",false).add("editable",edittype.equals("details")?"readonly":(edittype.equals("insert")?"":"")).add("relate","SrvProject"));
			}
			if(!(edittype.equals("details")?"readonly":(edittype.equals("insert")?"editable":"editable")).equals("none")){
				propertys.add(Utils.add("name","slGroup").add("caption","所属角色").add("input","query").add("primary",false).add("length",8).add("notNull",false).add("unique",false).add("editable",edittype.equals("details")?"readonly":(edittype.equals("insert")?"editable":"editable")).add("relate","SrvGroup"));
			}
			if(!(edittype.equals("details")?"readonly":(edittype.equals("insert")?"":"")).equals("none")){
				propertys.add(Utils.add("name","slLimit").add("caption","权 限 值").add("input","select").add("primary",false).add("length",4).add("notNull",true).add("unique",false).add("editable",edittype.equals("details")?"readonly":(edittype.equals("insert")?"":"")));
			}
			submits.add(Utils.add("name","submiter").add("value","完成").add("title","完成").add("icon","icon-weibiaoti8"));
		}
	}
  
}